<template>
  <div class="login">
    <!-- 标题 -->
    <bus-nav-bar class="nav_title" title="登录"></bus-nav-bar>
    <!-- 登录 -->
    <van-form class="form" ref="form">
      <!-- 手机号码 -->
      <van-field
        v-model="form.mobile"
        placeholder="请输入手机号"
        name="mobile"
        :rules="rules.mobile"
        @touchstart.native.stop="show = true"
        clearable
        @blur="mobileBlur"
      >
        <template #left-icon>
          <span class="iconfont">&#xea41;</span>
        </template>
      </van-field>
      <!-- 手机键盘 -->
      <van-number-keyboard
        v-model="form.mobile"
        :show="show"
        :maxlength="11"
        @blur="show = false"
      />
      <!-- 验证码 -->
      <van-field
        v-model="form.code"
        placeholder="请输入验证码"
        name="code"
        :rules="rules.code"
        @touchstart.native.stop="showCode = true"
        clearable
        @blur="codeBlur"
      >
        <template #left-icon>
          <span class="iconfont">&#xe68a;</span>
        </template>
        <template #button>
          <van-button
            round
            size="small"
            color="#1a171a"
            @click="getCode"
            v-model="total"
            >{{ send }}</van-button
          >
        </template>
      </van-field>
      <!-- 手机键盘 -->
      <van-number-keyboard
        v-model="form.code"
        :show="showCode"
        :maxlength="10"
        @blur="showCode = false"
        hide-on-click-outside
      />
      <!-- 登录按钮 -->
      <div style="margin: 10px 25px">
        <van-button
          round
          block
          size="small "
          native-type="button"
          color="#1a171a"
          class="submit"
          @click="login"
          >提交</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import { userLogin } from '@/api/login.js'
import { setLocal } from '@/utils/token.js'
export default {
  props: { title: String },
  data () {
    return {
      total: 60, // 时间倒计时
      send: '发送验证码',
      show: false, // 手机号键盘
      showCode: false, // 验证码键盘
      form: {
        // 13999999999
        mobile: '13999999999',
        // 246810
        code: '246810'
      },
      rules: {
        mobile: [
          {
            required: true,
            message: '请输入正确的手机号码',
            validator: val => {
              return /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/.test(
                val
              )
            },
            trigger: 'onBlur'
          }
        ],
        code: [
          {
            required: true,
            message: '请输入正确的验证码',
            validator: val => {
              return /^[0-9]{6}$/.test(val)
            },
            trigger: 'onBlur'
          }
        ]
      }
    }
  },
  methods: {
    // 获取验证码
    getCode () {
      this.$refs.form
        .validate('mobile')
        .then(() => {
          // 手机号码正确
          this.$toast.loading({
            message: '加载中',
            duration: 1000
          })
          // 倒计时
          this.total--
          const timeId = setInterval(() => {
            this.total--
            this.send = `${this.total}秒`
            if (this.total < 1) {
              clearInterval(timeId)
              this.send = '发送验证码'
              this.total = 60
            }
          }, 1000)
        })
        .catch(() => {
          // 手机号码失败
          this.$toast.fail('失败')
        })
    },
    // 登录 全局校验
    login () {
      this.$refs.form
        .validate()
        .then(() => {
          // 手机号码正确
          userLogin(this.form).then(res => {
            // 打印只有token
            // console.log(res)
            // 提示
            this.$toast.success('登录成功')
            // 保存token
            setLocal(res.data.token)
            // 保存到vuex
            this.$store.commit('setIsLogin', true)
            // 跳转路由
            this.$router.push('/routerTabbar/userInfo')
          })
        })
        .catch(() => {
          // 手机号码失败
          this.$toast.fail('登录失败')
        })
    },
    // 手机号码输入框失去焦点收起键盘
    mobileBlur () {
      this.show = false
    },
    // 验证码输入框失去焦点收起键盘
    codeBlur () {
      this.showCode = false
    }
  }
}
</script>

<style lang="less" scoped>
.login {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  min-width: 375px;
  z-index: 10;
  zoom: 1;
  background-color: #fff;
  background: url('~@/assets/login.png');
  background-repeat: no-repeat;
  background-size: cover;
  -webkit-background-size: cover;
  -o-background-size: cover;
  background-position: center 0;
  .nav_title {
    background-color: #30404b;
    width: 100%;
    user-select: none;
    text-align: center;
    height: 46px;
    line-height: 46px;
    .van-nav-bar__title {
      color: #fff;
    }
  }
  .form {
    padding: 30px 40px 5px;
    opacity: 0.5;
  }
}
</style>
